<?php
/**
 * Created by PhpStorm.
 * User: stupboy
 * Date: 2018/2/4
 * Time: 0:40
 */

namespace app\api\controller\driver;
use app\api\model;
use think\Hook;
/**
 * @name【推送管理】
 * @baseurl【driver.index】
 * @file【driver/Push.php】
 */
class Push extends Base
{
    /**
     * @name【绑定推送】
     * @desc【绑定推送】
     * @uri【addBind】
     * @method【post】
     * @param【
     *  dtoken $ string $ abcd $ 校验码 $ 1 #
     * push_user_id $ string $ abcdasdfasdfaf $ 极光用户标识id $ 1 #
     * device_token $ string $ abac $ 设备唯一标识 $ 1#
     * pkg_name $ string $ xdg.1233 $ 包名$ 1#
     * device_brand $ string $ 小米 $ 设备品牌 $ 1#
     * device_model $ string $ 2S $ 设备型号 $ 1#
     * device_version $ string $ 8 $系统版本 $1 #
     * device_type $ int $ 1 $ 设备类型 1安卓 2苹果
     * 】
     * @return【
     * code $ int $ number|1-1 $ 校验码#
     * msg $ string $  $ 错误信息
     * 】
     */
    public function addBind(){
        $string = 'push_user_id#推送ID@require/device_token/';
        $string.= 'device_type#设备类型@require|number/pkg_name#包名@require/device_brand#手机品牌@require/device_model#设备型号@require/device_version#系统版本@require';
        $data   = $this->getBatchParam($string);
        $data['use_type']       = 1;
        $exist    = model\PushBind::get(['user_id'=>$this->driverInfo['user_id'],'use_type'=>1]);
        $pushBind = new model\PushBind();
        if(!empty($exist)){
            $pushBind->save($data,['user_id'=>$this->driverInfo['user_id'],'use_type'=>1]);
            $msg   = $data['device_brand'].$data['device_model'];
            $param = ['type'=>'driver','id'=>$exist->push_user_id,'pid'=>$this->driverInfo['driver_id'],'msg'=>$msg];
            Hook::listen('change_device',$param);
            return $this->retSucc();
        }else{
            $data['user_id']        = $this->driverInfo['user_id'];
            if($pushBind->validate(true)->save($data)){
                return $this->retSucc();
            }else{
                return $this->retError($pushBind->getError());
            }
        }
    }
}